package predict.automata;

import java.util.ArrayList;
import java.util.List;

import weibo4j.model.WeiboException;

public class Predict {
	public Network network; 
	public int totalCount = 0;
	public int t = 0;
	
	public Predict(List<String> initialList, InterestItem theme) throws WeiboException {
		network = new Network(theme);
		
		for (String uid : initialList) {
			network.addPostedUserCell(uid);
		}
		do {
			for (String uid : network.getPostedUserList()) {
				UserCell uc = network.getAllUserCell().get(network.getUserCell(uid));
				for (String followerId : uc.getFollowersId()) {
					UserCell follower = network.getAllUserCell().get(network.getUserCell(followerId));
					follower.addNeighbour(uid);
					follower.setNeighbourCount(follower.getNeighbourCount()+1);
					if (!network.getToPostUserList().contains(followerId) && follower.isNot_to_post() == false) {
						network.addToPostUserCell(followerId);
					}
					else if (network.getToPostUserList().contains(followerId)) {
						follower.setTimeInfluenceDegree(follower.getTimeInfluenceDegree() / 2);
						follower.setTimeWindow(follower.getTimeWindow()+1);
					}
				}
			}
			List<String> removeList = new ArrayList<String>();
			for (String uid : network.getToPostUserList()) {
				UserCell uc = network.getAllUserCell().get(network.getUserCell(uid));
				double p = computeProbability(uc);
				uc.setProbability(p);
				double r = Math.random();
				if (r < p) {
					uc.setNot_to_post(true);
					network.addPostedUserCell(uid);
				}
				else if (p < 0.01 || uc.getTimeWindow() >= 5) {
					removeList.add(uid);
					uc.setNot_to_post(true);
				}
			}
			for (String rid : removeList) {
				network.removeFromToPostList(rid);
			}
			t++;
			System.out.println("time = " + t + " , totalCount = " + network.getPostedUserList().size() + ", toPost = " + network.getToPostUserList().size());
		} while(network.getToPostUserList() != null && t < 10);
	}
	
	public double computeProbability(UserCell uc) {
		return Math.random() * Math.pow(uc.getTimeInfluenceDegree(), uc.getProbability());
		//TODO
	}
	
	public static void main(String[] argv) throws WeiboException {
		List<String> initialList = new ArrayList<String>();
		initialList.add("1714694317");
		Predict predictSystem = new Predict(initialList, InterestItem.体育);
	}
		
}
